package com.calvin.lingting.adapter;


import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.calvin.lib.EventDispatch;
import com.calvin.lib.json.JSONObject;
import com.calvin.lib.utils.CUtils;
import com.calvin.lingting.MainActivity;
import com.calvin.lingting.R;
import com.calvin.lingting.config.LrtsConfig;
import com.calvin.lingting.service.SerializationCenter;

public class ChapterListViewAdapter extends BaseAdapter {

    Context mContext;
    JSONObject mBookInfo;
    AbsListView.LayoutParams mLayoutParams;

    public ChapterListViewAdapter(Context context, JSONObject bookInfo) {
        mContext = context;
        mBookInfo = bookInfo;
        mBookInfo = mBookInfo == null ? new JSONObject() : mBookInfo;
        float chapterHeight = mContext.getResources().getDimension(R.dimen.d_title_size);
        mLayoutParams = new AbsListView.LayoutParams(-1, (int) chapterHeight);

        Log.i("debug", "" + mBookInfo.get(LrtsConfig.K_DOWNDLOAD));

    }

    public JSONObject getBookInfo() {
        return mBookInfo;
    }

    public void setBookInfo(JSONObject mBookInfo) {
        this.mBookInfo = mBookInfo;
    }

    public void onDwonloadChapter(View view) {
        Log.i("debug", "download:" + view.getTag());
        view.setBackground(null);
        SerializationCenter.shared().downloadBook(mBookInfo, (Integer) view.getTag() + 1);
    }

    @Override
    public int getCount() {
        if (!mBookInfo.containsKey(LrtsConfig.K_CHAPTER_LIST)) {
            return 0;
        }
        return mBookInfo.getJSONObject(LrtsConfig.K_CHAPTER_LIST).size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.list_chapter_item, null);
        }
        String tmp = MainActivity.sharedCurrent().getString(R.string.s_download_chapter);
        CUtils.cast(convertView.findViewById(R.id.book_chapter), TextView.class).setText(String.format(tmp + "-%s", position + 1, mBookInfo.getStringValue(LrtsConfig.K_NAME)));
        View button = convertView.findViewById(R.id.book_download);
        button.setTag(position);
        button.setEnabled(!isDownload(position));
        button.setBackgroundResource(button.isEnabled() ? R.drawable.iconfont_download : 0);
        EventDispatch.shared().put(button, EventDispatch.CListen.CLICK, this, "onDwonloadChapter");
        convertView.setLayoutParams(mLayoutParams);
        return convertView;
    }

    private boolean isDownload(int position) {
        return mBookInfo.containsKey(LrtsConfig.K_DOWNDLOAD) && mBookInfo.getJSONArray(LrtsConfig.K_DOWNDLOAD).contains(position + 1);
    }
}
